package com.yfbao.sharding.mybatisp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfbao.sharding.mybatisp.po.Test;
import com.yfbao.sharding.mybatisp.po.User;
import com.yfbao.sharding.mybatisp.service.inter.ITestService;
import com.yfbao.sharding.mybatisp.service.inter.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	
	@Autowired
	private ITestService testService;
	
	@GetMapping("/tests")
	public Object list() {
		return testService.list();
	}
	
	@GetMapping("/tests/add")
	public Object add() {
		for (long i = 0; i < 100; i++) {
			Test testUser = new Test();
			testUser.setCity("深圳");
			testUser.setName("李四");
			testUser.setTableIndex((int)i);
			testService.save(testUser);
		}
		return "success";
	}
	
	@GetMapping("/tests/one/{id}")
	public Object get(@PathVariable Long id) {
		LambdaQueryWrapper<Test> eq = new QueryWrapper<Test>().lambda().eq(Test::getId, id);
		return testService.list(eq);
	}
	
	@GetMapping("/tests/query")
	public Object get(@RequestParam("name") String name) {
		LambdaQueryWrapper<Test> eq = new QueryWrapper<Test>().lambda().eq(Test::getName, name);
		Page<Test> page =new Page<>(1,20);
		return testService.page(page,eq);
	}

	@GetMapping("/tests/count")
	public Object getAllCount() {
		return testService.count();
	}


}
